package com.jinlejia.feedback.question.adapter.question;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;

import com.jinlejia.feedback.databinding.FbAdapterQuestionSolutionBinding;

import java.util.ArrayList;
import java.util.Map;

/**
 * 解决方案适配器
 */

public class SolutionHolderHelper {

    public static void bindViewHolder(KeyHelper.EventListener eventListener,RecyclerView.Adapter adapter, ViewHolder holder, ArrayList<Map<String, Object>> mData, int position){
        final FbAdapterQuestionSolutionBinding binding = DataBindingUtil.bind(holder.itemView);
        final Map<String, Object> map = mData.get(position);
        WebSettings mSetting = binding.webView.getSettings();
        mSetting.setJavaScriptEnabled(true);
        if (TextUtils.isEmpty(binding.webView.getUrl())){
            binding.webView.loadData(String.valueOf(map.get("solutionContent")),"text/html; charset=UTF-8", null);
        }

        binding.timeTextView.setText(String.valueOf(map.get("gmtModified")));
        //region 用户信息
        KeyHelper.showUserInfo(binding.includeInfo,map.get("problemManageName"),map.get("problemManagePortrait"),
                map.get("problemManageDept"),map.get("problemManageDuty")
        );
        //endregion

        if (Boolean.parseBoolean(String.valueOf(map.get(KeyHelper.LocalIsShowAll)))){
            binding.showAllInfoTextView.setVisibility(View.GONE);
            ViewGroup.LayoutParams params = binding.webView.getLayoutParams();
            params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            binding.webView.setLayoutParams(params);
        }else {
            binding.showAllInfoTextView.setVisibility(View.VISIBLE);
            ViewGroup.LayoutParams params = binding.webView.getLayoutParams();
            params.height = binding.webView.getMinimumHeight();
            binding.webView.setLayoutParams(params);
        }
        binding.showAllInfoTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                map.put(KeyHelper.LocalIsShowAll,true);
                binding.webView.setVisibility(View.VISIBLE);
                ViewGroup.LayoutParams params = binding.webView.getLayoutParams();
                params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                binding.webView.setLayoutParams(params);
                binding.showAllInfoTextView.setVisibility(View.GONE);
            }
        });

    }


    public static class ViewHolder extends RecyclerView.ViewHolder{

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }
}
